require 'rbof'

class App < OF::SimpleApp
  include OF
  
  def setup
    setBackgroundAuto false
    setVerticalSync true
    @cam = VideoGrabber.new
    @cam.initGrabber 320, 240
    @pixels = Array.new 320 * 240 * 3
  end
  
  def update
    @cam.grabFrame
  end
  
  def draw
    if @cam.isFrameNew
      background 255, 255, 255
      draw_dots
      draw_src
    end
  end
  
  def draw_dots
    setColor 0
    pushMatrix
      translate 320, 0, 0
      pixels = @cam.getPixels
      4.step 320, 8 do |i|
        4.step 240, 8 do |j|
          v = pixels[(j * 320 + i) * 3]
          v = 1 - v / 255.0
          circle i, j, 10 * v
        end
      end 
    popMatrix
  end
  
  def draw_src
    setColor 0xffffff
    @cam.draw 0, 0
  end
  
  def keyPressed(key)
    @cam.videoSettings if key.chr == 's'
  end
end

OF.setupOpenGL 640, 240, OF::WINDOW
OF.runApp App.new